#include "dbms.h"

DBMS::DBMS()
{
    //Print("DBMS() constructor");

    CurrentDB = "";
    DBMap.clear();

    //DBMap["Profile"] = true;

    ConnectedDBMap.clear();
    //SaveInfoToFile();
    //return;

    LoadInfoFromFileInfo();
}

DBMS::~DBMS()
{
    SaveInfoToFile();
}

int DBMS::SaveInfoToFile()
{
    //Output(message, "SaveIn");
    QFile file( "dbms/info" );

    if ( !file.open( QIODevice::WriteOnly ) ) return write_error;

    QDataStream fout( &file );
    fout.setVersion( QDataStream::Qt_4_4 );

    //Print( "Writing items to file" );
    fout << DBMap.size();
    for( QMap<QString, bool>::iterator i = DBMap.begin(); i != DBMap.end(); i++ )
	fout << i.key();

    file.close();

    return ok;
}

int DBMS::LoadInfoFromFileInfo()
{
    //Print("LoadInfoFromFile() is executed");
    QFile file( "dbms/info" );

    if ( !file.open( QIODevice::ReadOnly ) ) return read_error;

    QDataStream fin( &file );
    fin.setVersion( QDataStream::Qt_4_4 );

    int count;
    QString buffer = "";
    fin >> count;

    for( int i = 0; i < count; i++ ){
	fin >> buffer;
        //Print(buffer);
	DBMap[ buffer ] = true;
    }

    file.close();

    return ok;
}

bool DBMS::isConnected( const QString& DBName )
{
    return ConnectedDBMap.contains( DBName );
}

int DBMS::ConnectDatabase( const QString& DBName )
{
    //Print("ConnectDatabase() is executed");
    if ( !DBMap.contains( DBName ) ) return find_error;

    if ( !isConnected( DBName ) ){
	DataBase DB;
	ConnectedDBMap[ DBName ] = DB;
	return ConnectedDBMap[ DBName ].LoadDatabaseFromFile( DBName );
    } else return ok;
}

int DBMS::DisconnectDatabse( const QString& DBName )
{
    if ( !DBMap.contains( DBName ) ) return find_error;

    if (isConnected( DBName )){
        ConnectedDBMap[ DBName ].SaveDatabaseToFile( DBName );
        ConnectedDBMap.remove( DBName );
        if ( CurrentDB == DBName ) CurrentDB = "";
        return ok;
    } else return find_error;
}

int DBMS::SetCurrentDatabase( const QString& DBName )
{
    //Print("SetCurrentDatabase() is executed");
    if ( !DBMap.contains( DBName ) ) return find_error;

    if ( isConnected( DBName ) ){
	CurrentDB = DBName;
	return ok;
    } else return find_error;
}

DataBase* DBMS::GetDatabase( const QString& DBName )
{
    //Print("GetDatabase() is executed");
    if ( !DBMap.contains( DBName ) ) return 0;

    //Print(DBName + QString(" presents!"));
    if ( isConnected( DBName ) ){
	return &ConnectedDBMap[ DBName ];
    } else return 0;
}

DataBase* DBMS::CurrentDatabase()
{
    //Print("CurrentDatabase() is executed");
    return GetDatabase( CurrentDB );
}

int DBMS::CreateDatabase( DataBaseInfo& Info )
{
    //Output( message, "CreateDatabase() is executed" );

    if ( DBMap.contains( Info.DBName ) )
        return dup_error; else
            DBMap[Info.DBName] = true;

    Info.ItemsNumber = 0;
    if ( Info.VisibleNumber > Info.RecordsNumber ) return range_error;
    if ( Info.KeyField > Info.RecordsNumber ) return range_error;

    //seting default column width (length of headers)
    int buffer;
    Info.ColumnWidth.clear();
    for (int i=0; i<Info.VisibleNumber; i++){
        buffer = Info.ColumnHeaders[i].size() * CellK + CellSpace;
        Info.ColumnWidth.push_back( buffer );
    }


    //Output( message, "creating DB" );
    DataBase DB( Info );
    //Output( message, "DB has been created" );
    return DB.SaveDatabaseToFile( Info.DBName );
}

int DBMS::RemoveDatabase( const QString& DBName )
{
    if ( !DBMap.contains( DBName ) ) return find_error;

    //Database exists.
    if ( isConnected( DBName ) ) DisconnectDatabse( DBName );

    DBMap.remove( DBName );
    SaveInfoToFile();

    return 0;
}

int DBMS::RemoveAllDatabases()
{
    DBMap.clear();
    SaveInfoToFile();
}

void DBMS::RestoreDefaultDatabases()
{
    DBMap.clear();
    DBMap["Materials"] = true;
    DBMap["Stock"] = true;
    DBMap["Orders"] = true;
    DBMap["Income"] = true;
    DBMap["Furniture"] = true;
    DBMap["Profile"] = true;

    SaveInfoToFile();
}

void DBMS::ChangeDatabaseInfo()
{
    ConnectDatabase("Income");
    DataBase *db = GetDatabase( "Income" );
    db->setColumnHeader( 0 , QObject::tr("Date") );
    db->setColumnHeader( 1 , QObject::tr("Status") );
    db->setColumnHeader( 2 , QObject::tr("Description") );
    //db->setColumnHeader( 3 , QObject::tr("Format") );
    db->SaveDatabaseToFile();
}
